/*
 * 显示控制
 */
function showRights_box(){
	document.getElementById('rights_box_null').style.display = 'none';
	document.getElementById('rights_box_full').style.display = '';
	document.getElementById('rights_box_btn').style.display = '';
}
/*
 * 全选method
 * param 目标节点Id
 */
function choose_all(element_id){
	//获取全选按钮对象
	var parent_box = document.getElementById(element_id);
	//获取所有下级 checkbox
	var child_boxes = document.getElementsByName(element_id);
	if(parent_box.checked==true){
		//循环填充
		for(var i=0;i<child_boxes.length;i++){
			child_boxes[i].checked=true;
		}
	}else{
		for(var i=0;i<child_boxes.length;i++){
			child_boxes[i].checked=false;
		}
	}
}
/*
 * 全选method
 * param 目标节点Name
 */
function pad_all(element_name){
	var child_boxes = document.getElementsByName(element_name);
	var count = 0;
	//循环计数
	for(var i=0;i<child_boxes.length;i++){
		if(child_boxes[i].checked){
			count++;
		}
	}
	//当所有下级ckbox的数量和checked为true的ckbox相同
	if(count==child_boxes.length){
		document.getElementById(element_name).checked=true;
	}else{
		document.getElementById(element_name).checked=false;
	}
}
/*
 * checkBox 整理打包
 * return map
 */
function getRights(elementName){
	//获取所有input标签
	var inputBox = document.getElementsByTagName('input');
	var parent_ckbox_arr = new Array();
	var child_ckbox_arr= new Array();
	var tempArr;
	var targetJson= {};
	for(var i=0;i<inputBox.length;i++){
		//筛选出checkbox，将父级和子级分组填装。
		if(inputBox[i].type=='checkbox'){
			if(inputBox[i].name==elementName){
				parent_ckbox_arr.push(inputBox[i]);
			}else{
				child_ckbox_arr.push(inputBox[i]);
			}
		}
	}
	for(var i=0;i<parent_ckbox_arr.length;i++){
		//以父级的checked的值（boolean）为规则，将父级和子级对应填装到json中
		if(parent_ckbox_arr[i].checked){
			//全选状态
			targetJson[parent_ckbox_arr[i].id] = "check_all";
		}else{
			tempArr = new Array();
			for(var k=0;k<child_ckbox_arr.length;k++){
				if(parent_ckbox_arr[i].id == child_ckbox_arr[k].name&&child_ckbox_arr[k].checked){
					tempArr.push(child_ckbox_arr[k].id);
				}
			}
			if(tempArr!=""){
				targetJson[parent_ckbox_arr[i].id] = tempArr;
			}else{
				//未选中
				targetJson[parent_ckbox_arr[i].id] = "unchecked";
			}
		}
	}
	return targetJson;
}

var oldrn;
var oldra;
function role_edit(roleId){
	var current_rn = document.getElementById('tblRole.roleName').value;
	var current_ra = document.getElementById('tblRole.roleAbstract').value;
	var p = document.getElementById('role_edit_prompt');
	if(current_rn===""||current_ra===""){
		alert("数据不能为空");
	}else if(oldrn===current_rn&&oldra===current_ra){
		alert("数据未变更");
	}else{
		$.ajax({
			type:'post',
			url:'role_do_edit',
			data:"roleId="+roleId+'&roleName='+current_rn+"&roleAbstract="+current_ra,
			success:function(){
				p.style.color = 'green';
				p.innerHTML = "数据修改成功";
			},
			error:function(){
				p.style.color = 'red';
				p.innerHTML = "提交失败，请联系管理员。";
			}
		});
		oldrn = document.getElementById('tblRole.roleName').value;
		oldra = document.getElementById('tblRole.roleAbstract').value;
	}
}
function role_add_rights(elementName,roleId){
	var p = document.getElementById('rights_prompt');
	//将获得的数组转换成Json
	var targetJson = JSON.stringify(getRights(elementName));
	$.ajax({
		type:'post',
		url:'role_add_rights',
		data:"roleId="+roleId+"&rights_codes="+targetJson,
		success:function(){
			p.style.color = "green";
			p.innerHTML = "权限变更成功!";
		},
		error:function(){
			p.style.color = "red";
			p.innerHTML = "权限变更失败!";
		}
	});
}